package booyah.service;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import booyah.application.Event;
import booyah.application.User;
import booyah.application.Position;

/**
 *
 */
public class EventServiceImpl extends JpaDaoSupport implements EventService {

    public void createEvent(final Event event) {
        if (event.getId() != null) {
            throw new IllegalArgumentException("Not a new event.");
        }
        getJpaTemplate().persist(event);
    }

    public void updateEvent(Event event) {
        JpaTemplate jpaTemplate = getJpaTemplate();
        jpaTemplate.persist(jpaTemplate.contains(event) ? event : jpaTemplate.merge(event));
    }

    public void deleteEvent(Event event) {
        if (event == null) {
            throw new IllegalArgumentException("Event must be specified.");
        }
        JpaTemplate jpaTemplate = getJpaTemplate();
        jpaTemplate.remove(jpaTemplate.contains(event) ? event : jpaTemplate.merge(event));
    }

    public List<Event> getAllEvents() {
        return null; // TODO proper impl
    }

    public List<Event> getAllFutureEvents() {
        return null; // TODO proper impl
    }

    public void updateEvent(Event event, Position position) {
        JpaTemplate jpaTemplate = getJpaTemplate();
        if (position.getId() == -1) {
            position = jpaTemplate.contains(position) ? position : jpaTemplate.merge(position);
        }
        jpaTemplate.persist(jpaTemplate.contains(event) ? event : jpaTemplate.merge(event));
    }

    public void removePositionFromEvent(Event event, Position position) {
        if (event == null) {
            throw new IllegalArgumentException("Event must be specified.");
        }
        if (position == null) {
            throw new IllegalArgumentException("Position must be specified.");
        }
        JpaTemplate jpaTemplate = getJpaTemplate();
        event = jpaTemplate.contains(event) ? event : jpaTemplate.merge(event);
        event.removePosition(position);
        jpaTemplate.persist(event);
    }

    public List<Event> getAllUnfilledEvents() {
        return null; // TODO proper impl
    }

    public List<Event> getAllFilledEvents() {
        return null; // TODO proper impl
    }

    public List<Event> findAllEvents() {
        return getJpaTemplate().find("from Event e");
    }

    public Event findEventByEventId(Long id) {
        return getJpaTemplate().find(Event.class, id);
    }

    @SuppressWarnings("unchecked")
    public List<Event> findEventsByStartDate(Timestamp startTime) {
        return getJpaTemplate().find("from Event e where e.startTime > ?", startTime);
    }

    public List<Event> findEventsForAdministrator(User administrator) {
        return getJpaTemplate().find("from Event e where e.administrator = ?", administrator);
    }

    public Position findPositionByPositionId(Long id) {
        return getJpaTemplate().find(Position.class, id);
    }

    public void updatePosition(Position position) {
        JpaTemplate jpaTemplate = getJpaTemplate();
        jpaTemplate.persist(jpaTemplate.contains(position) ? position : jpaTemplate.merge(position));
    }


}
